//
//  GaoTableView.m
//  DuTe
//
//  Created by Gikki Ares on 2020/12/15.
//  Copyright © 2020 vgemv. All rights reserved.
//



#import "JsTableView.h"
#import "JsNoDataView.h"
#import "JsMjRefresh.h"
#import "JsTableView_.h"

//TODO: 首先确定一个全屏模式下的最佳效果,然后根据tableView的大小调整transform就可以做到适配了.

@interface JsTableView ()
{
	UIView * mView_noData;
	//已加载的page数量
	int mi_loadedPage;
	
	JsTableViewStyle * mStyle;
}
	
@end


@implementation JsTableView

#pragma mark LifeCycle

- (instancetype)init {
	if(self = [super init]) {
		[self commonInit];
	}
	return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
	if(self = [super initWithCoder:coder]) {
		[self commonInit];
	}
	return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
	if(self = [super initWithFrame:frame]) {
		[self commonInit];
	}
	return self;
}

//容器本身不处理事件.
//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//	return [_tableView hitTest:point withEvent:event];
//}

- (void)commonInit {
	JsTableView_ * tableView = [[JsTableView_ alloc]initWithFrame:self.bounds];
	[self addSubview:tableView];
	_tableView = tableView;
	if (@available(iOS 13.0, *)) {
		_tableView.automaticallyAdjustsScrollIndicatorInsets = NO;
	} else {
			// Fallback on earlier versions
	}
	//关闭自动计算inset,这个会让jsmj_footer的位置不对.
	if (@available(iOS 11.0, *)) {
		_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
	} else {
			// Fallback on earlier versions
	}
	if (@available(iOS 13.0, *)) {
		_tableView.automaticallyAdjustsScrollIndicatorInsets = NO;
	} else {
			// Fallback on earlier versions
	}
	[self initMjRefresh];
	
	//
	self.backgroundColor = [UIColor clearColor];
	_tableView.backgroundColor = [UIColor clearColor];
	//隐藏空白的横线
	_tableView.tableFooterView = [UIView new];
	_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	

}



- (void)setJsDelegate:(id<JsTableViewDelegate,UITableViewDelegate,UITableViewDataSource>)jsDelegate {
	_jsDelegate = jsDelegate;
	_tableView.delegate = _jsDelegate;
	_tableView.dataSource = _jsDelegate;
	//如果代理没有设置noDataView,则使用默认的.
	
	if(_jsDelegate&&[_jsDelegate respondsToSelector:@selector(jsTableViewSetNoDataView:)]) {
			self->mView_noData = [self->_jsDelegate jsTableViewSetNoDataView:self];
	};
	if(!mView_noData) {
		mView_noData = [[JsNoDataView alloc] init];
		mView_noData.frame = self.bounds;
	}
	
	//设置样式
	if(_jsDelegate&&[_jsDelegate respondsToSelector:@selector(jsTableViewSetStyle:)]) {
		mStyle = [_jsDelegate jsTableViewSetStyle:self];
	}
	
	if(!mStyle) {
		mStyle = [JsTableViewStyle defaultStyle];
	}
	_tableView.backgroundColor = mStyle.mColor_tableViewBackground;
	mView_noData.backgroundColor = mStyle.mColor_defaultNoDataViewBackground;
	_tableView.showsVerticalScrollIndicator = mStyle.mb_isShowVerticalScrollBar;
	//设置view的大小.
	[self->_tableView addSubview:self->mView_noData];
	dispatch_async(dispatch_get_main_queue(), ^{
		[self reloadData];
	});
	
	
}

- (void)initMjRefresh {
		// 下拉刷新
	_tableView.jsMj_header= [JsMjRefreshNormalHeader headerWithRefreshingBlock:^{
		//请求数据
		
		if(self.jsDelegate&&[self.jsDelegate respondsToSelector:@selector(jsTableViewRefresh:)]) {
			[self.jsDelegate jsTableViewRefresh:self];
		}
		else {
			//没有设置代理的默认方法
			[self->_tableView.jsMj_header endRefreshing];
			[self->_tableView reloadData];
		}
	}];
	
	_tableView.jsMj_footer= [JsMjRefreshBackGifFooter footerWithRefreshingBlock:^{
		
		if(self.jsDelegate&&[self.jsDelegate respondsToSelector:@selector(jsTableViewLoadMore:forPage:)]) {
			[self.jsDelegate jsTableViewLoadMore:self forPage:self->mi_loadedPage+1];
		}
		else {
			[self->_tableView.jsMj_footer endRefreshing];
			[self->_tableView reloadData];
		}
	}];
}

- (void)finishLoadMoreWithIsSuccess:(BOOL)isSuccess dataCount:(int)dataCount {
	if(isSuccess) {
		if(dataCount) {
			mi_loadedPage ++;
			[self->_tableView.jsMj_footer endRefreshing];
		}
		else {
			[self->_tableView.jsMj_footer endRefreshingWithNoMoreData];
		}
	}
	else {
		[self->_tableView.jsMj_footer endRefreshing];
	}
	
}

//YES,增加页数
- (void)finishRefreshWithIsSuccess:(BOOL)isSuccess dataCount:(int)dataCount {
	if(isSuccess) {
		if(dataCount > 0) {
			//获取到了新数据数据
			self->mi_loadedPage = 1;
			[self->_tableView.jsMj_header endRefreshing];
			[self->_tableView.jsMj_footer endRefreshing];
		}
		else {
			mi_loadedPage = 0;
			[self->_tableView.jsMj_header endRefreshing];
			[self->_tableView.jsMj_footer endRefreshingWithNoMoreData];
		}
	}
	else {
		[self->_tableView.jsMj_header endRefreshing];
	}
}

- (void)setNoMoreData {
	[self->_tableView.jsMj_footer endRefreshingWithNoMoreData];
}



- (void)layoutSubviews {
	[super layoutSubviews];
//	NSLog(@"%@,self frame is %@,The frame is %@ ,contentInset is %@",NSStringFromClass(self.class),NSStringFromCGRect(self.frame),NSStringFromCGRect(self.tableView.frame),NSStringFromUIEdgeInsets(self.tableView.contentInset));
	_tableView.frame = self.bounds;
	mView_noData.frame = self.bounds;
	[_tableView bringSubviewToFront:mView_noData];
}

#pragma mark 2 interface
- (void)setTag:(NSInteger)tag {
	[super setTag:tag];
	_tableView.tag = tag;
}

- (int)loadedPageCount {
	return mi_loadedPage;
}


//要重新加载数据时,使用这边的reloadData,进行是否为空的检查.
- (void)reloadData {
	[_tableView reloadData];
	[self checkEmpty];
}

- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
	[_tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
	[self checkEmpty];
}

- (void)checkEmpty {
	if(mView_noData) {
		if ([self isEmpty]) {
			if(mStyle.mb_isShowEmptyView) {
				mView_noData.hidden = NO;
			}
			else {
				mView_noData.hidden = YES;
			}
		}
		else {
			mView_noData.hidden = YES;
		}
	}
}

- (BOOL)isEmpty {
	NSInteger section = _tableView.numberOfSections;
	BOOL isEmpty = YES;
	for (int i=0; i<section; i++) {
		if([_tableView numberOfRowsInSection:i]>0) {
			isEmpty = NO;
			break;
		}
	}
	return isEmpty;
}


@end
